package com.software.library_conquer;

import android.app.ActionBar;
import android.app.Activity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.jeremyfeinstein.slidingmenu.lib.SlidingMenu;


public class AchievementActivity extends Activity {

    protected SlidingMenu side_drawer;
    RecyclerView mRecyclerView;
    RecyclerView.LayoutManager mLayoutManager;
    MyAdapter mAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_achievement);
        initSlidingMenu();

        ActionBar actionBar = getActionBar();
        actionBar.setDisplayHomeAsUpEnabled(true);

        mRecyclerView = (RecyclerView) findViewById(R.id.my_recycler_view);

        mRecyclerView.setHasFixedSize(true);

        mLayoutManager = new LinearLayoutManager(this);
        mRecyclerView.setLayoutManager(mLayoutManager);

        mAdapter = new MyAdapter();
        mRecyclerView.setAdapter(mAdapter);
    }

    protected void initSlidingMenu() {
        side_drawer = new DrawerView(this).initSlidingMenu();
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.menu_achievement, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        int id = item.getItemId();

        if (id == R.id.action_settings) {
            return true;
        }

        if (id == android.R.id.home) {
            if(!side_drawer.isMenuShowing())// || !side_drawer.isSecondaryMenuShowing())
                side_drawer.showMenu();
            return true;
        }

        return super.onOptionsItemSelected(item);
    }

    public class MyAdapter extends RecyclerView.Adapter<MyAdapter.ViewHolder> {
        private String[] mDataset;

        // Provide a reference to the views for each data item
        // Complex data items may need more than one view per item, and
        // you provide access to all the views for a data item in a view holder
        public class ViewHolder extends RecyclerView.ViewHolder {
            // each data item is just a string in this case
            public ImageView img_avicon;
            public TextView tv_avtitle;
            public TextView tv_avdescr;
            public ViewHolder(View v) {
                super(v);
                img_avicon = (ImageView)v.findViewById(R.id.img_avicon);
                tv_avtitle = (TextView)v.findViewById(R.id.tv_avtitle);
                tv_avdescr = (TextView)v.findViewById(R.id.tv_avdescr);
            }
        }

        // Provide a suitable constructor (depends on the kind of dataset)
        public MyAdapter() {

        }
        // Provide a suitable constructor (depends on the kind of dataset)
        public MyAdapter(String[] myDataset) {
            mDataset = myDataset;
        }

        // Create new views (invoked by the layout manager)
        @Override
        public MyAdapter.ViewHolder onCreateViewHolder(ViewGroup parent,
                                                       int viewType) {
            // create a new view
            View v = LayoutInflater.from(parent.getContext())
                    .inflate(R.layout.achievement_item, parent, false);
            // set the view's size, margins, paddings and layout parameters

            return new ViewHolder(v);
        }

        // Replace the contents of a view (invoked by the layout manager)
        @Override
        public void onBindViewHolder(ViewHolder holder, int position) {
            // - get element from your dataset at this position
            // - replace the contents of the view with that element
            switch (position) {
                case 0:
                    holder.img_avicon.setImageResource(R.drawable.head_benj);
                    holder.tv_avtitle.setText("连续签到3天");
                    holder.tv_avdescr.setText("小菜一碟");
                    break;
                case 1:holder.img_avicon.setImageResource(R.drawable.head_bili);
                    holder.tv_avtitle.setText("连续签到10天");
                    holder.tv_avdescr.setText("小试牛刀");
                    break;
                case 2:holder.img_avicon.setImageResource(R.drawable.head_van);
                    holder.tv_avtitle.setText("连续签到10000天");
                    holder.tv_avdescr.setText("不毕业了_(′π`」 ∠)_");
                    break;

            }

        }

        // Return the size of your dataset (invoked by the layout manager)
        @Override
        public int getItemCount() {
            return 3;
        }
    }
}
